package com.aliyun.heiyu.powermaster.server.rbac.dao.impl;


import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.server.rbac.dao.SysAuditManagementsDao;
import com.aliyun.heiyu.powermaster.server.rbac.object.entity.SysAuditManagements;
import java.util.List;
import java.util.Map;

public class SysAuditManagementsDaoImpl implements SysAuditManagementsDao {

    @Override
    public void saveAuditManagement(SysAuditManagements entity) {
        String sql = "insert into sys_audit_managements(business_module,event_type,operation_type,user_name,start_time,end_time,description) value (?,?,?,?,?,?,?);";
        Object[] kk = new Object[7];
        kk[0] = entity.getBusinessModule();
        kk[1] = entity.getEventType();
        kk[2] = entity.getOperationType();
        kk[3] = entity.getUserName();
        kk[4] = entity.getStartTime();
        kk[5] = entity.getEndTime();
        kk[6] = entity.getDescription();

        //boolean update = JdbcUtil.update(sql, kk);
        Integer id = JdbcUtil.insert(sql, kk);
        entity.setId(id);
    }

    @Override
    public boolean updateAuditManagement(SysAuditManagements entity) {
        String sql = "update sys_audit_managements set business_module = ?,event_type = ?,operation_type = ?,user_name = ?,start_time=?,end_time=?,description=? where id = ?";
        Object[] kk = new Object[8];
        kk[0] = entity.getBusinessModule();
        kk[1] = entity.getEventType();
        kk[2] = entity.getOperationType();
        kk[3] = entity.getUserName();
        kk[4] = entity.getStartTime();
        kk[5] = entity.getEndTime();
        kk[6] = entity.getDescription();
        kk[7] = entity.getId();
        return JdbcUtil.update(sql, kk);
    }

    @Override
    public boolean deleteAuditManagement(Integer auditManagementId) {
        String sql = "delete from sys_audit_managements where id = ?";
        Object[] kk = new Object[1];
        kk[0] = auditManagementId;
        return JdbcUtil.update(sql, kk);
    }

    @Override
    public List<SysAuditManagements> getAuditManagements(Integer startNum, Integer endNum, String businessModule) {
        String sql = "select id,business_module businessModule,event_type eventType,operation_type operationType,user_name userName,start_time startTime,end_time endTime,description" +
            "  from sys_audit_managements";
        Object[] kk = null;
        if (!StringUtil.isEmpty(businessModule)){
            sql = sql + " where business_module = ? ";
            kk = new Object[3];
            kk[0] = businessModule;
            kk[1] = startNum;
            kk[2] = endNum;
        }else {
            kk = new Object[2];
            kk[0] = startNum;
            kk[1] = endNum;
        }
        sql = sql + "   limit ?,?";

        return JdbcUtil.queryForList(sql, kk,SysAuditManagements.class);
    }

    @Override
    public Integer getAuditManagementsCount(String businessModule) {
        String sql = "select count(1) total from sys_audit_managements ";
        Object[] kk = null;
        if (!StringUtil.isEmpty(businessModule)){
            sql = sql + " where business_module = ? ";
            kk = new Object[1];
            kk[0] = businessModule;
        }
        Map<String, String> map = JdbcUtil.queryForMap(sql, kk);
        return  map != null && map.get("total") != null ? Integer.valueOf(map.get("total")) : 0;
    }

    @Override
    public SysAuditManagements getAuditManagementById(Integer auditManagementId) {

        String sql = "select id,business_module businessModule,event_type eventType,operation_type operationType,user_name userName,start_time startTime,end_time endTime,description" +
            " from sys_audit_managements where id = ?";
        Object[] kk = new Object[1];
        kk[0] = auditManagementId;
        return JdbcUtil.queryForBean(sql,kk,SysAuditManagements.class);
    }

    @Override
    public SysAuditManagements getAuditManagementByUserName(String userName) {
        String sql = "select id,business_module businessModule,event_type eventType,operation_type operationType,user_name userName,start_time startTime,end_time endTime,description" +
            " from sys_audit_managements where user_name = ?";
        Object[] kk = new Object[1];
        kk[0] = userName;
        return JdbcUtil.queryForBean(sql,kk,SysAuditManagements.class);
    }
}
